﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace 玩具商城管理系统.Tproduct
{
    public partial class Tinsert : Form
    {
        public Tinsert()
        {
            InitializeComponent();
        }
        SqlDataAdapter daProduct, dalog;
        DataSet ds = new DataSet();
        public static string path_source = "";
        void init()
        {
            DB.GetCn();
            string str = "select * from commodity";
            string dtr = "select * from log";
            daProduct = new SqlDataAdapter(str, DB.cn);
            dalog = new SqlDataAdapter(dtr, DB.cn);
            daProduct.Fill(ds, "product_info");
            dalog.Fill(ds, "log_info");

        }
        void showAll()
        {
            DataView dvProduct =new DataView(ds.Tables["product_info"]);
            dgvProduct.DataSource = dvProduct;
        }
        private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtId.Text = dgvProduct.CurrentRow.Cells["commodity_id"].Value.ToString();
            cbosort.Text = dgvProduct.CurrentRow.Cells["sort_name"].Value.ToString();
            txtdesc.Text = dgvProduct.CurrentRow.Cells["commodity_desc"].Value.ToString();
            txtbook.Text = dgvProduct.CurrentRow.Cells["commodity_book"].Value.ToString();
            txtchengb.Text = dgvProduct.CurrentRow.Cells["commodity_chengb"].Value.ToString();
            txtinvent.Text = dgvProduct.CurrentRow.Cells["commodity_invent"].Value.ToString();
            txtmoney.Text = dgvProduct.CurrentRow.Cells["commodity_money"].Value.ToString();
            txtname.Text = dgvProduct.CurrentRow.Cells["commodity_name"].Value.ToString();
            try
            {
                pic_Product.Image = Image.FromFile(Application.StartupPath
                    + dgvProduct.CurrentRow.Cells["commodity_picture"].Value.ToString());
                pic_Product.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch
            {
                pic_Product.Image = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.gif.jpg");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog() == DialogResult.OK)
            {
                path_source = ofd.FileName;
                pic_Product.Image=Image.FromFile(path_source);  
                pic_Product.SizeMode=PictureBoxSizeMode.StretchImage;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(txtname.Text=="")
            {
                MessageBox.Show("商品名称不能为空");
            }
            else if (txtchengb.Text=="")
            {
                MessageBox.Show("成本价格不能为空");
            }
            else if (txtinvent.Text=="")
            {
                MessageBox.Show("库存不能为空");
            }
            else
            {
                DialogResult dr = MessageBox.Show("您确定要对此商品进行修改吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if(dr==DialogResult.OK)
                {
                    string filename;
                    string fileFolder;
                    string dateTime = "";
                    filename = Path.GetFileName(path_source);
                    dateTime += DateTime.Now.Year.ToString();
                    dateTime += DateTime.Now.Month.ToString();
                    dateTime += DateTime.Now.Day.ToString();
                    dateTime += DateTime.Now.Hour.ToString();
                    dateTime += DateTime.Now.Minute.ToString();
                    dateTime += DateTime.Now.Second.ToString();
                    filename = dateTime + filename;
                    fileFolder = Directory.GetCurrentDirectory() + "\\" + "Prod_Images\\"
                        + cbosort.Text + "\\";
                    fileFolder += filename;

                    int a = dgvProduct.CurrentRow.Index;
                    string str = dgvProduct.Rows[a].Cells["commodity_id"].Value.ToString();
                    DataRow[] ProRows = ds.Tables["product_info"].Select("commodity_id='" + str + "'");
                    ProRows[0]["sort_name"]=cbosort.Text;
                    ProRows[0]["commodity_name"] = txtname.Text;
                    ProRows[0]["commodity_desc"] = txtdesc.Text;
                    ProRows[0]["commodity_money"] = decimal.Parse(txtmoney.Text); 
                    ProRows[0]["commodity_invent"] = decimal.Parse(txtinvent.Text); ;
                    ProRows[0]["commodity_chengb"] = int.Parse(txtchengb.Text);
                    ProRows[0]["commodity_book"] = int.Parse(txtbook.Text);
                    if(path_source!="")
                    {
                      File.Copy(path_source, fileFolder, true);
                        ProRows[0]["commodity_picture"] = "\\Prod_Images\\" + cbosort.Text + "\\"
                            + filename;
                    }
                    DataRow drlog = ds.Tables["log_info"].NewRow();
                    drlog["username"] = login.username;
                    drlog["type"] = "修改";
                    drlog["action_date"] = DateTime.Now;
                    drlog["action_table"] = "commodity表";
                    ds.Tables["log_info"].Rows.Add(drlog);
                    try
                    {
                        SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProduct);
                        daProduct.Update(ds, "product_info");
                        SqlCommandBuilder dbLog = new SqlCommandBuilder(dalog);
                        dalog.Update(ds, "log_info");
                        MessageBox.Show("修改成功");
                        path_source = "";
                        pic_Product.SizeMode = PictureBoxSizeMode.StretchImage;
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    DB.cn.Close();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pic_Product_Click(object sender, EventArgs e)
        {

        }

        private void Tinsert_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“toyStroeDataSet1.commodity”中。您可以根据需要移动或移除它。
            this.commodityTableAdapter.Fill(this.toyStroeDataSet1.commodity);
            init();
            showAll();
        }

        
    }
}
